package com.lushu.chapitre1.states {
import com.lushu.chapitre1.core.Configuration;
import com.lushu.chapitre1.data.database.ItemDatabase;
import com.lushu.chapitre1.data.enums.DataStoreKey;
import com.lushu.chapitre1.graphics.controls.LoadingBar;
import com.lushu.chapitre1.resources.GameDataLoader;
import com.sfdk.core.ConfigReader;
import com.sfdk.core.DataStore;
import com.sfdk.core.GameProxy;
import com.sfdk.core.abstract.AbstractState;
import com.sfdk.data.ServerKeyPair;
import com.sfdk.events.ConfigEvent;
import com.sfdk.events.RemoteEvent;
import com.sfdk.events.ResourcesEvent;
import com.sfdk.graphics.core.MovieClipEx;
import com.sfdk.net.php.RemotingService;
import com.sfdk.resources.ResourcesLoader;

import flash.display.Graphics;

import flash.display.Sprite;
import flash.events.Event;
import flash.utils.getDefinitionByName;

public final class GameInitState extends AbstractState {
    private var _configReader:ConfigReader;
    private var _resourcesLoader:ResourcesLoader;
    private var _gameDataLoader:GameDataLoader;

    private var _loadingScreen:Sprite;
    private var _loadingBar:LoadingBar;

    public function GameInitState(gameProxy:GameProxy, parameters:Object = null) {
        super(gameProxy, new MovieClipEx(), parameters);
    }

    /** State Mechanism **/

    protected override function initState(parameters:Object = null):void {
        super.initState();

        _loadingScreen = new Sprite();
        _view.addChild(_loadingScreen);

        _loadingBar = new LoadingBar();


        _configReader = new ConfigReader();
        _configReader.addEventListener(ConfigEvent.CONFIG_READ, onConfigLoaded, false, 0, true);

        super.stateLoaded();
    }

    public override function endState():void {
        super.endState();
        _configReader = null;
    }

    /** Event Handlers **/

    private function onConfigLoaded(e:Event):void {
        var configuration:Configuration = new Configuration(_configReader);
        DataStore.store(DataStoreKey.CONFIGURATION, configuration);

        // Let's instantiate the remote connection service
        var currentServer:ServerKeyPair = configuration.currentServer;

        // Let's load the real resources
        _resourcesLoader = new ResourcesLoader(_loadingBar);
        _resourcesLoader.addEventListener(ResourcesEvent.SPLASHSCREEN_LOADED, onSplashScreenLoaded, false, 0, true);
        _resourcesLoader.addEventListener(ResourcesEvent.RESOURCES_LOADED, onResourcesLoaded, false, 0, true);
    }

    private function onSplashScreenLoaded(e:Event):void {
        //var loadingScreenCls:Class = getDefinitionByName("Loading_Screen") as Class;
        var loadingScreen : Sprite = new Sprite();
        var g : Graphics = loadingScreen.graphics;
        g.beginFill(0xFF0000);
        g.drawRect(0,0,200,200);
        g.endFill();
        _loadingScreen.addChild(loadingScreen);

        _view.attachChild(_loadingBar, 0, 558 - 5 - 12 - 5, "loadingBar", "none", "center");
    }

    private var _resourcesSource:int = 1;

    private function onResourcesLoaded(e:Event):void {
        _resourcesSource--;
        if (_resourcesSource == 0) {
            _view.removeChild(_loadingBar);
            _loadingBar = null;
            _resourcesLoader = null;
            _gameProxy.setState(LoadDataState, {firstTime : true});
        }
    }

}
}